// utils/authAlert/authAlert.js
let app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    showModel: true
  },

  /**
   * 组件的方法列表
   */
  methods: {
    getUserInfo: function(){
      app.func.Get(true, app.api.get_userInfo, { 'userId': app.globalData.userId }, function (data) {
        app.globalData.userSweets = data.integral
        app.globalData.userInfo = {
          "city": data.city,
          "country": data.country,
          "nickName": data.name,
          "province": data.province,
          "avatarUrl": data.avatar
        };
      });
    },
    bindCatch() {
      app.loginPromise.catch(() => {
        var that = this;
        wx.getSetting({
          success: (res) => {
            //判断用户是否授权。已经授权不需要弹框
            if (res.authSetting['scope.userInfo']) {
              console.log('已经授权', res)
            } else {
              console.log('未授权', res)
              that.setData({
                showModel: false
              })
            }
          },
          fail: function () {
            that.setData({
              showModel: false
            })
          }
        })
      });
    },
    //获取用户信息新接口
    agreeGetUser: function (e) {
      try {
        wx.clearStorageSync()
      } catch (e) {
      }
      var self = this;
      console.log("点击授权")
      if (e.detail.errMsg != "getUserInfo:ok"){
        console.log("点击授权，授权失败")
        return
      }
      console.log("点击授权，授权成功")
      this.setData({
        showModel: true
      })
      console.log("开始登陆")
      getApp().loginService()
      getApp().loginPromise.then(() => {
        self.triggerEvent("authSuccess")
      });
    },
  },
})
